// 获取页面尺寸，用于判断是移动端还是PC端
let pageWidth = document.body.clientWidth;
let verificationCodeBtn, submitBtn, returnBtn, inputEmail = "";

if (pageWidth >= 768) {
    verificationCodeBtn = document.getElementsByClassName("getVerificationCode")[0];
    submitBtn = document.getElementsByClassName("submitBtn")[0];
    inputEmail = document.getElementsByClassName("email")[0];
} else {
    verificationCodeBtn = document.getElementsByClassName("getVerificationCode")[1];
    submitBtn = document.getElementsByClassName("submitBtn")[1];
    inputEmail = document.getElementsByClassName("email")[1];

    returnBtn = document.getElementById("returnBtn");
    /*返回个人中心页面*/
    returnBtn.addEventListener('click', () => {
        location.href = route.userInfo;
    });
}


let verCode = 0;
let count = 60;
let email = sessionStorage.getItem('userEmail');
inputEmail.value = email;

verificationCodeBtn.addEventListener('click', () => {
    let inputEmail = "";
    if (pageWidth >= 768) {
        inputEmail = document.getElementsByClassName("email")[0].value;
    } else {
        inputEmail = document.getElementsByClassName("email")[1].value;
    }
    if (inputEmail.length === 0) {
        alertBySweet("邮箱不能为空");
    } else if (inputEmail.indexOf("@") == -1 || inputEmail.indexOf(".com") == -1) {
        alertBySweet("请输入正确格式的邮箱！");
    } else {
        let timer = setInterval(function () {
            verificationCodeBtn.innerHTML = "剩余" + count + "s";
            count--;
            if (count === -1) {
                verificationCodeBtn.innerHTML = "获取验证码";
                clearInterval(timer);
                count = 60;
            }
        }, 1000);
        //给邮箱发送验证码
        if (count === 60) {// 60秒发送一次验证码
            ajax({
                type: 'POST',
                url: api.sendEmail,
                async: true,
                contentType: "application/json;charset=utf-8",
                data: {
                    email: inputEmail,
                },
                success: function (data) {
                    let res = JSON.parse(data);
                    if (res.statusCode === 200) {
                        alertBySweet("邮件发送成功，请查收！");
                        verCode = res.data.verCode;
                    } else {
                        alertBySweet("邮件发送失败，请稍后重试！");
                    }
                },
                error: function () {
                    alertBySweet("发送失败");
                }
            });
        }
    }
});

submitBtn.addEventListener('click', function () {
    let inputVerCode, inputEmailValue, userName = "";
    if (pageWidth >= 768) {
        inputVerCode = document.getElementsByClassName("verCode")[0].value;
    } else {
        inputVerCode = document.getElementsByClassName("verCode")[1].value;
    }
    if (inputVerCode === verCode) {//验证码正确
        if (pageWidth >= 768) {
            inputEmailValue = document.getElementsByClassName("email")[0].value
            userName = document.getElementsByClassName("name")[0].value;//防止用户发完验证码之后又改了邮箱
        } else {
            inputEmailValue = document.getElementsByClassName("email")[1].value;//防止用户发完验证码之后又改了邮箱
            userName = document.getElementsByClassName("name")[1].value;//防止用户发完验证码之后又改了邮箱
        }
        if (inputEmailValue != email) {
            alertBySweet("请使用当前用户的邮箱！");
            return;
        }

        //给邮箱发送验证码
        ajax({
            type: 'POST',
            url: api.modifyName,
            // url: '/api/modifyName',
            async: true,
            contentType: "application/json;charset=utf-8",
            data: {
                email: inputEmail,
                name: userName
            },
            success: function (data) {
                let res = JSON.parse(data);
                if (res.statusCode === 200) {
                    alertBySweet("修改成功！");
                } else {
                    alertBySweet("修改失败，请检查邮箱输入是否正确！");
                }
            },
            error: function () {
                alertBySweet("修改失败");
            }
        });
    } else {
        alertBySweet("验证码输入错误！");
    }
});
